layui.use(['table', 'admin', 'ax', 'func','form','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;
    var laydate = layui.laydate;

    /**
     * 管理
     */
    var BtyMtyq = {
        tableId: "btyMtyqTable"
    };

    //渲染时间选择框
    laydate.render({
        elem: '#createTime',
        range: true,
        tiger: 'click',done: function(value, date){
            //alert(value);
            $('#createTime').val(value);
            BtyMtyq.search();
    }
        // max: Feng.currentDate()
    });

    form.on('select(state)', function (data) {
        BtyMtyq.search();
    });
    form.on('select(type)', function (data) {
        BtyMtyq.search();
    });


    /**
     * 检索店名
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyMtyq/dmList");
    var languageResult = languageAjax.start();
    $.each(languageResult, function (index, val) {
        $("#dm").append("<option value='" + val.dm + "'>" + val.dm + "</option>");
    });
    form.render('select');


    /**
     * 初始化表格的列
     */
    BtyMtyq.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'mdid', sort: true, title: '门店id'},
            {field: 'dm', sort: true, title: '店名'},
            {field: 'name', sort: true, title: '团单名称'},
            {
                field: 'type', align: "center", sort: true, title: '团单类型',templet: function (d) {
                    let a = "";
                    if (d.type == "1") {
                        a = "代金券";
                    } else if (d.type == "2") {
                        a = "套餐券";
                    }else if (d.type == "4") {
                        a = "霸王券";
                    }
                    return a;
                }
            },
            {field: 'code', sort: true, title: '券码'},
            {field: 'state', sort: true, title: '券码状态',templet: function (d) {
                    let a = "";
                    if (d.state == "10") {
                        a = "已核销";
                    } else if (d.state == "25") {
                        a = "已撤销";
                    }else if (d.state == "11") {
                        a = "已核销";
                    }else if (d.state == "20") {
                        a = "不可用";
                    }
                    return a;
                }},
            {field: 'amount', sort: true, title: '实收金额',totalRow:true},
            {field: 'dkje', sort: true, title: '抵扣金额',totalRow:true},
            // {field: 'createName', sort: true, title: '验券人'},
            // {field: 'createPhone', sort: true, title: '验券人手机号'},
             {field: 'createTime', sort: true, title: '验券时间'},
            // {field: 'createUser', sort: true, title: '创建人'},
            //{field: 'updateTime', sort: true, title: '验券时间'},
            // {field: 'updateUser', sort: true, title: '修改人'},
            // {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyMtyq.search = function () {
        var queryData = {};

        queryData['mdid'] = $('#mdid').val();
        queryData['dm'] = $('#dm').val();
        queryData['type'] = $('#type').val();
        //queryData['code'] = $('#code').val();
        queryData['state'] = $('#state').val();
        queryData['cjsj'] = $('#createTime').val();

        table.reload(BtyMtyq.tableId, {
            where: queryData, page: {curr: 1}
        });
    };




    /**
     * 弹出添加对话框
     */
    BtyMtyq.openAddDlg = function () {
        func.open({
            title: '添加',
            content: Feng.ctxPath + '/btyMtyq/add',
            tableId: BtyMtyq.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      BtyMtyq.openEditDlg = function (data) {
          func.open({
              title: '修改',
              content: Feng.ctxPath + '/btyMtyq/edit?id=' + data.id,
              tableId: BtyMtyq.tableId
          });
      };

    // 清空按钮点击事件
    $('#btnReset').click(function () {
        BtyMtyq.reset();
    })

    // 导入Txt按钮点击事件
    // $('#btnTxt').click(function () {
    //     BtyMtyq.txt();
    // });
    BtyMtyq.txt = function () {
        func.open({
            height: 200,
            title: '券码管理导入Txt',
            content: Feng.ctxPath + '/btyMtyq/upload',
            tableId: BtyMtyq.tableId
        });
    };

    /**
     * 点击清空按钮
     */
    BtyMtyq.reset = function () {
        $('#mdid').val('');
        $('#dm').val('');
        $('#type').val('');
        $('#code').val('');
       $('#state').val('');
        form.render();
        BtyMtyq.search();
    };
    /**
     * 导出excel按钮
     */
    BtyMtyq.exportExcel = function () {
        var checkRows = table.checkStatus(BtyMtyq.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyMtyq.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyMtyq/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyMtyq.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyMtyq.tableId,
        url: Feng.ctxPath + '/btyMtyq/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        totalRow:true,
        cols: BtyMtyq.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyMtyq.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    BtyMtyq.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyMtyq.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyMtyq.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyMtyq.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BtyMtyq.onDeleteItem(data);
        }
    });
});
